<?php


namespace App\Http\Controllers\Admin;


use App\Exceptions\ValidatorException;
use App\Http\Controllers\Controller;
use App\Http\Repositories\Repository;
use Illuminate\Http\Request;

class RecommendController extends Controller
{
    private $repository;
    public function __construct(Repository $repository)
    {
        $this->repository = $repository;
    }

    /**
     * 获取
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function getRecommendList(Request $request)
    {
        //验证规则
        $rules = [
            'type' => 'nullable|string',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = \Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->getRecommendList($validator->getData());
    }

    /**
     * 添加
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function addRecommend(Request $request)
    {
        //验证规则
        $rules = [
            'type' => 'nullable|string',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = \Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->addRecommend($validator->getData());
    }

    public function addPraise(Request $request)
    {
        //验证规则
        $rules = [
            'member_uuid' => 'required|string',
            'article_id' => 'required|string',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = \Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->addPraise($validator->getData());
    }

    public function addCount(Request $request)
    {
        //验证规则
        $rules = [
//            'member_uuid' => 'required|string',
//            'article_id' => 'required|string',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = \Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->addCount($validator->getData());
    }

    /**
     * 点赞/取消
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function praiseCount(Request $request)
    {
//        $a = $request->input('debug');
//        dd($a);
        //验证规则
        $rules = [
            'member_uuid' => 'required|string',
            'article_id' => 'required|string',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        $request->offsetSet('type', 1);
        //验证结果 错误就抛出异常
        $validator = \Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->addCount($validator->getData());
    }

    /**
     * 踩/取消
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function despiseCount(Request $request)
    {
        //验证规则
        $rules = [
            'member_uuid' => 'required|string',
            'article_id' => 'required|string',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        $request->offsetSet('type', 2);

        //验证结果 错误就抛出异常
        $validator = \Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->addCount($validator->getData());
    }

    /**
     * 收藏/取消
     * @param Request $request
     * @return mixed
     * @throws ValidatorException
     */
    public function favoritesCount(Request $request)
    {
        //验证规则
        $rules = [
            'member_uuid' => 'required|string',
            'article_id' => 'required|string',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        $request->offsetSet('type', 3);
        //验证结果 错误就抛出异常
        $validator = \Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->addCount($validator->getData());
    }

}
